﻿using System;
using static CyberU;
using static CyberU.UI;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using UnityEngine.Serialization;

public class GraphBoundary : RectComponent
{
    public Graph graph;
    public GameObject border;
    public BoxCollider2D boxCollider;

    public void Awake()
    {
        border = FindGameObjectInChild("Boundary",gameObject);
        boxCollider = AddOrGetComponent<BoxCollider2D>(border);
        boxCollider.isTrigger = true;
    }

    public void Start()
    {
        RefreshBoundary();
    }

    public void RefreshBoundary()
    {
        Vector3[] corners = new Vector3[4];
        rectTransform.GetWorldCorners(corners);
        float left = corners[0].x;
        float right = corners[2].x;
        float bottom = corners[0].y;
        float top = corners[2].y;
        
        border.transform.SetParent(transform.parent);
        border.transform.localPosition = Vector3.zero;
        float width = Mathf.Abs(right - left);
        float height = Mathf.Abs(top - bottom);
        boxCollider.size = new Vector2(width, height);
        boxCollider.offset = new Vector2((left + right) / 2, (top + bottom) / 2);
    }
}